package com.ynet.middleground.user.model;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynet.middleground.user.bean.QueryHistoryReq;
import com.ynet.middleground.user.dao.EnterpriseBasicInfoHistoryMapper;
import com.ynet.middleground.user.dao.EnterpriseEnclosureHistoryMapper;
import com.ynet.middleground.user.dao.EnterpriseUserInfoHistoryMapper;
import com.ynet.middleground.user.dao.NaturalPersonBaseInformationHistoryMapper;
import com.ynet.middleground.user.dao.UserBaseInformationHistoryMapper;
import com.ynet.middleground.user.dto.*;
import com.ynet.middleground.user.entity.AuthorizationbookurlHistory;
import com.ynet.middleground.user.entity.ColdchainHistory;
import com.ynet.middleground.user.entity.EnterpriseBasicInfoHistory;
import com.ynet.middleground.user.entity.EnterpriseEnclosure;
import com.ynet.middleground.user.entity.EnterpriseEnclosureHistory;
import com.ynet.middleground.user.entity.EnterpriseUserInfoHistory;
import com.ynet.middleground.user.entity.NaturalPersonBaseInformationHistory;
import com.ynet.middleground.user.entity.UserBaseInformationHistory;
import com.ynet.middleground.user.utils.BeanUtil;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @program: 2019-10-12_userCenter
 * @description: 查询历史记录model
 * @author: DaiGaoLe
 * @create: 2019-10-23 17:55
 **/
@Component
public class QueryHistoryRecordModel extends ServiceImpl<UserBaseInformationHistoryMapper, UserBaseInformationHistory> {

    @Autowired
    EnterpriseUserInfoHistoryMapper enterpriseUserInfoHistoryMapper;

    @Autowired
    NaturalPersonBaseInformationHistoryMapper naturalPersonBaseInformationHistoryMapper;

    @Autowired
    EnterpriseBasicInfoHistoryMapper enterpriseBasicInfoHistoryMapper;

    @Autowired
    private Mapper dozerBeanMapper;
    
    @Autowired
    ColdChainHistoryModel coldChainHistoryModel;
    
    @Autowired
    AuthorizationbookurlHistoryModel authorizationbookurlHistoryModel;
    
    @Autowired
    EnterpriseEnclosureHistoryMapper enterpriseEnclosureHistoryMapper;

    // 查询用户信息
    public QueryHistoryRecordInfoDto queryUserInfo(QueryHistoryReq requestObject) {
        // 声明变量 begin
        QueryHistoryRecordInfoDto queryHistoryRecordInfoDto = new QueryHistoryRecordInfoDto();
        Integer userId = requestObject.getUserId();
        QueryWrapper<UserBaseInformationHistory> queryWrapper = new QueryWrapper<>();
        // 声明变量 end
        queryWrapper.eq("user_id", userId);
        String sql = "select max(creation_date) as creation_date from ur_user_base_information_history a where user_id = " + userId;
        queryWrapper.inSql("creation_date", sql);
        List<UserBaseInformationHistory> userBaseInformationHistories = this.getBaseMapper().selectList(queryWrapper);
        if (userBaseInformationHistories.size()>0) {
            UserBaseInformationHistory userBaseInformationHistory = userBaseInformationHistories.get(0);
            UserBaseInfoHistoryDto userBaseInfoHistoryDto = dozerBeanMapper.map(userBaseInformationHistory, UserBaseInfoHistoryDto.class);
            queryHistoryRecordInfoDto.setUserBaseInfoHistoryDto(userBaseInfoHistoryDto);
            Integer id = userBaseInformationHistory.getId();
            // 企业用户
            EnterpriseUserInfoHistory enterpriseUserInfoHistory = enterpriseUserInfoHistoryMapper.selectById(id);
            if (enterpriseUserInfoHistory != null) {
                EnterpriseUserInfoHistoryDto enterpriseUserInfoHistoryDto = dozerBeanMapper.map(enterpriseUserInfoHistory, EnterpriseUserInfoHistoryDto.class);
                queryHistoryRecordInfoDto.setEnterpriseUserInfoHistoryDto(enterpriseUserInfoHistoryDto);
                userBaseInfoHistoryDto = dozerBeanMapper.map(enterpriseUserInfoHistory, UserBaseInfoHistoryDto.class);
                queryHistoryRecordInfoDto.setUserBaseInfoHistoryDto(userBaseInfoHistoryDto);
            }
            // 企业  2020-08-05 宋一帆 因企业信息修改与信息修改分离，两表历史信息无法关联
//            EnterpriseBasicInfoHistory enterpriseBasicInfoHistory = enterpriseBasicInfoHistoryMapper.selectById(id);
//            if (enterpriseBasicInfoHistory != null) {
//                EnterpriseBasicInfoHistoryDto enterpriseBasicInfoHistoryDto = dozerBeanMapper.map(enterpriseBasicInfoHistory, EnterpriseBasicInfoHistoryDto.class);
//                queryHistoryRecordInfoDto.setEnterpriseBasicInfoHistoryDto(enterpriseBasicInfoHistoryDto);
//            }
            // 自然人
            NaturalPersonBaseInformationHistory naturalPersonBaseInformationHistory = naturalPersonBaseInformationHistoryMapper.selectById(id);
            if (naturalPersonBaseInformationHistory != null) {
                NaturalPersonBaseInfoHistoryDto naturalPersonBaseInfoHistoryDto = dozerBeanMapper.map(naturalPersonBaseInformationHistory, NaturalPersonBaseInfoHistoryDto.class);
                queryHistoryRecordInfoDto.setNaturalPersonBaseInfoHistoryDto(naturalPersonBaseInfoHistoryDto);
                userBaseInfoHistoryDto = dozerBeanMapper.map(naturalPersonBaseInformationHistory, UserBaseInfoHistoryDto.class);
                queryHistoryRecordInfoDto.setUserBaseInfoHistoryDto(userBaseInfoHistoryDto);
            }
        }
        return queryHistoryRecordInfoDto;
    }
    
 // 查询企业信息
    public EnterpriseBasicInfoHistoryDto queryBasicInfo(QueryHistoryReq requestObject) {
    	Integer userId = requestObject.getUserId();
    	QueryWrapper<EnterpriseBasicInfoHistory> queryWrapperEBI = new QueryWrapper<>();
    	EnterpriseBasicInfoHistoryDto enterpriseBasicInfoHistoryDto = new EnterpriseBasicInfoHistoryDto();
        // 声明变量 end
    	queryWrapperEBI.eq("user_id", userId);
        String sqlEBI = "select max(creation_date) as creation_date from ur_enterprise_basic_info_history a where user_id = " + userId;
        queryWrapperEBI.inSql("creation_date", sqlEBI);
        List<EnterpriseBasicInfoHistory> enterpriseBasicInfoHistorys = enterpriseBasicInfoHistoryMapper.selectList(queryWrapperEBI);
        // 企业
        if (enterpriseBasicInfoHistorys.size()>0) {
        	EnterpriseBasicInfoHistory enterpriseBasicInfoHistory = enterpriseBasicInfoHistorys.get(0);
            enterpriseBasicInfoHistoryDto = dozerBeanMapper.map(enterpriseBasicInfoHistory, EnterpriseBasicInfoHistoryDto.class);
            //2020-08-04 宋一帆增加冷链和法定代表人授权委托书url查询
            QueryWrapper<ColdchainHistory> coldQueryWrapper = new QueryWrapper<>();
            coldQueryWrapper.eq("id", enterpriseBasicInfoHistory.getId());
            enterpriseBasicInfoHistoryDto.setColdChainList((BeanUtil.mapList(dozerBeanMapper,coldChainHistoryModel.list(coldQueryWrapper), ColdchainHistoryDTO.class)));
            AuthorizationbookurlHistory authorizationbookurlHistory = authorizationbookurlHistoryModel.getById(enterpriseBasicInfoHistory.getId());
            if(authorizationbookurlHistory!=null) {
            String authorizationBookUrl = dozerBeanMapper.map(authorizationbookurlHistory, AuthorizationbookurlHistoryDTO.class).getAuthorizationBookUrl();
            enterpriseBasicInfoHistoryDto.setAuthorizationBookUrl(authorizationBookUrl);
            }
            LambdaQueryWrapper<EnterpriseEnclosureHistory> queryEEH = new LambdaQueryWrapper<EnterpriseEnclosureHistory>();
            queryEEH.eq(EnterpriseEnclosureHistory::getId, enterpriseBasicInfoHistory.getId());
            enterpriseBasicInfoHistoryDto.setEnclosureLists((BeanUtil.mapList(dozerBeanMapper,
                enterpriseEnclosureHistoryMapper.selectList(queryEEH), EnterpriseEnclosureHistoryDTO.class)));
        }
        return enterpriseBasicInfoHistoryDto;
    }
}
